package com.skyworth.ui.customview;

import android.content.Context;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * Created by luwei on 15-11-13.
 * 水平滚动的简单listview,不支持view复用
 */
public class HSimpleListView extends HorizontalScrollView implements ISimpleListView{
    private SimpleListView listView = null;
    private Rect rect = new Rect();
    private ScrollNextListener scrollNextListener = null;
    public HSimpleListView(Context context) {
        super(context);
        init(context);
    }

    private void scrollToChild(View child){
        int scrollDelta = getNeeScrollDelta(child);
        Log.i("scroll","scrollDelta " + scrollDelta);
        if (scrollDelta != 0) {
            smoothScrollBy(scrollDelta, 0);
        }
    }

    public void setScrollNextListener(ScrollNextListener listener){
        this.scrollNextListener = listener;
    }

    public interface ScrollNextListener {
        public void willScrollTo(View current,View next,int len);
    }

    private int getNeeScrollDelta(View child){
        child.getDrawingRect(rect);

        /* Offset from child's local coordinates to ScrollView coordinates */
        offsetDescendantRectToMyCoords(child, rect);

        int scrollDelta = computeScrollDeltaToGetChildRectOnScreen(rect);
        return scrollDelta;
    }

    OnKeyListener focusKeyListener = new OnKeyListener() {
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            View next = null;
            switch (keyCode){
                case KeyEvent.KEYCODE_DPAD_LEFT:
                    next = listView.getChildAt(v.getId() -1);
                    if (next != null){
                        int len = getNeeScrollDelta(next);
                        if (scrollNextListener != null)
                            scrollNextListener.willScrollTo(v,next,len);
                    }
                    break;
                case KeyEvent.KEYCODE_DPAD_RIGHT:
                    next = listView.getChildAt(v.getId() + 1);
                    if (next != null){
                        int len = getNeeScrollDelta(next);
                        if (scrollNextListener != null)
                            scrollNextListener.willScrollTo(v,next,len);
                    }
                    break;
            }
            return false;
        }
    };


    @Override
    public void setAlwaysFocusPos(int pos) {
        listView.setAlwaysFocusPos(pos);
    }

    @Override
    public void setBoundaryListener(OnBoundaryListener listener) {
        listView.setBoundaryListener(listener);
    }

    public HSimpleListView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    @Override
    public int getFocusPos() {
        return listView.getFocusPos();
    }

    @Override
    public int getSelectedPos() {
        return listView.getSelectedPos();
    }

    public HSimpleListView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context);
    }

    @Override
    public void shieldKey(boolean left, boolean up, boolean right, boolean down) {
        listView.shieldKey(left,up,right,down);
    }

    private void init(Context context){
        listView = new SimpleListView(context);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        addView(listView, params);
        listView.setOrientation(LinearLayout.HORIZONTAL);
        listView.setFocusKeyListener(focusKeyListener);
    }

    public void setFillMode(SimpleListView.FILL_MODE mode){
        listView.setFillMode(mode);
    }

    public void setDivide(int divide){
        listView.setDivide(divide);
    }

    public void setDivideImageResource(int id){
        listView.setDivideImageResource(id);
    }

    public void setAdapter(BaseAdapter adapter){
        listView.setAdapter(adapter);
    }

    public void setAnimation(int id){
        listView.setAnimation(id);
    }

    public void setScrollBar(int id) {
        try {
            Field mScrollCacheField = View.class.getDeclaredField("mScrollCache");
            mScrollCacheField.setAccessible(true);
            Object mScrollCache = mScrollCacheField.get(this); // scr is your Scroll View

            Field scrollBarField = mScrollCache.getClass().getDeclaredField("scrollBar");
            scrollBarField.setAccessible(true);
            Object scrollBar = scrollBarField.get(mScrollCache);
            Method method = scrollBar.getClass().getDeclaredMethod("setHorizontalThumbDrawable", Drawable.class);
            method.setAccessible(true);
            // Set your drawable here.
            method.invoke(scrollBar, getResources().getDrawable(id));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public BaseAdapter getAdapter(){
        return listView.getAdapter();
    }

    public void setOnItemFocusChangeListener(OnItemFocusChangeListener listener){
        listView.setOnItemFocusChangeListener(listener);
    }

    public void setClickListener(OnClickListener listener){
        listView.setClickListener(listener);
    }

    public void setFocus(int pos){
        listView.setFocus(pos);
    }

    public void setSelected(int pos,boolean selected){
        listView.setSelected(pos,selected);
        if (selected){
            if (listView.getChildAt(pos) != null)
                scrollToChild(listView.getChildAt(pos));
        }
    }

    public void shieldAllKey(boolean key){
        listView.shieldAllKey(key);
    }

    public void enableFocus(boolean focus){
        listView.enableFocus(focus);
    }
}
